import utils.DButils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class text_insert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 1. 获取数据库连接
            connection = DButils.getConnection();
            // 2. 定义SQL
            String sql = "insert into student (sn, name, mail, class_id) values (?,?,?,?)";
            // 3. 定义SQL预处理对象
            statement = connection.prepareStatement(sql);
            // 4. 接收用户参数
            Scanner sc = new Scanner(System.in);

            System.out.println("请输入学号");
            String sn = sc.next();

            System.out.println("请输入姓名");
            String name = sc.next();

            System.out.println("请输入邮箱");
            String mail = sc.next();

            System.out.println("请输入班级名");
            long cId = Long.valueOf(sc.next());

            // 用真实数据填充
            statement.setString(1,sn);
            statement.setString(2,name);
            statement.setString(3,mail);
            statement.setLong(4,cId);

            // 获取结果
            int row = statement.executeUpdate();

            // 判断是否获取成功
            if (row == 1) {
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButils.close(null,statement,connection);
        }
    }
}
